package com.ilop.sthome.ui.activity.detail;

import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDetailOutdoorSirenBinding;

/**
 * @author skygge
 * @date 2019-12-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：户外警笛详情页
 */
public class OutDoorDetailActivity extends BaseDetailActivity<ActivityDetailOutdoorSirenBinding> {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_outdoor_siren;
    }

    @Override
    protected void initView() {
        super.initView();
        mDBind.detailIcon.setImageResource(R.mipmap.gs380);
    }

    @Override
    protected void initData() {
        super.initData();
        doStatusShow(mDevice.getDevice_status());
        showBattery();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.mute.setOnClickListener(view -> mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(),"50000000"));
        mDBind.operation.setOnClickListener(view -> mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(),"51000000"));
        mDBind.volume.setOnClickListener(view -> mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(),"52000000"));
    }

    private void showBattery(){
        try {
            if("1".equals(mDevice.getDevice_type().substring(0,1))){
                mDBind.quantityDesc.setVisibility(View.GONE);
                mDBind.quantityPosition.setVisibility(View.GONE);
            }else{
                mDBind.quantityDesc.setVisibility(View.VISIBLE);
                mDBind.quantityPosition.setVisibility(View.VISIBLE);
            }
        }catch (Exception e){
            Log.i(TAG,"data err");
        }
    }

    private void doStatusShow(String statusCode) {
        try {
            String signal = statusCode.substring(0,2);
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic(signal));

            String status = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), statusCode);
            int statusColor = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), statusCode);
            int quantity = Integer.parseInt(statusCode.substring(2,4),16);
            showDetail(statusColor, status, quantity);
        }catch (Exception e){
            showDetail(3, getString(R.string.offline), 0);
        }
    }

    private void showDetail(int statusColor, String text, int quantity){
        if (statusColor == 3){
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic("00"));
            mDBind.quantityPosition.setVisibility(View.GONE);
            mDBind.quantityDesc.setVisibility(View.GONE);
        }else {
            mDBind.quantityPosition.setVisibility(View.VISIBLE);
            mDBind.quantityDesc.setVisibility(View.VISIBLE);
            mDBind.quantityPosition.setImageResource(ConfigureUtil.choseQPic(quantity));
            mDBind.quantityDesc.setText(ConfigureUtil.choseLNum(quantity));
        }
        int background = ConfigureUtil.choseDeviceStateColor(statusColor);
        mDBind.detailLayout.setBackgroundResource(background);
        mDBind.showStatus.setText(text);
        if (statusColor == 0){
            AnimationDrawable mAnimation = (AnimationDrawable) mDBind.detailLayout.getBackground();
            mAnimation.start();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(TextUtils.isEmpty(mDevice.getSubdeviceName())){
            String title = getString(R.string.outdoor_siren) + mDevice.getDevice_ID();
            mDBind.detailName.setText(title);
        }else{
            mDBind.detailName.setText(mDevice.getSubdeviceName());
        }
    }

    @Override
    protected void refreshUI(String status) {
        super.refreshUI(status);
        doStatusShow(status);
    }
}
